﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace gsremote
{
    public partial class MainForm : Form
    {
        Native.EnumWindowsProc enumWindowsProc;
        Native.EnumWindowsProc enumChildWindowsProc;
        bool gsHwndNotFound;

        public MainForm()
        {
            InitializeComponent();
        }

        public bool EnumWindows(int hwnd, int lParam)
        {
            Native.EnumChildWindows(new IntPtr(hwnd), enumChildWindowsProc, IntPtr.Zero);
            return gsHwndNotFound;
        }

        public bool EnumChildWindows(int hwnd, int lParam)
        {
            StringBuilder ClassName = new StringBuilder(0x100);
            StringBuilder WindowText = new StringBuilder(0x400);
            int ret = Native.GetClassName(new IntPtr(hwnd), ClassName, ClassName.Capacity);
            if (ret != 0)
            {
                bool firefoxPlugin = string.Compare(ClassName.ToString(), "GeckoPluginWindow", true) == 0;
                bool chromePlugin = string.Compare(ClassName.ToString(), "NativeWindowClass", true) == 0;
                if (firefoxPlugin || chromePlugin)
                {
                    IntPtr parent = new IntPtr(hwnd);
                    do
                    {
                        IntPtr lastParent = parent;
                        parent = Native.GetParent(parent);
                        // check for top window
                        if (parent.ToInt64() == 0)
                            break;
                        // check for grooveshark in window text
                        ret = Native.GetWindowText(parent, WindowText, WindowText.Capacity);
                        if (ret != 0)
                        {
                            if (WindowText.ToString().IndexOf("Grooveshark") != -1)
                            {
                                gsHwndNotFound = false;
                                // send space bar message
                                IntPtr key = new IntPtr(Native.VK_SPACE);
                                if (Native.PostMessage(lastParent, (uint)Native.WM_KEYDOWN, key, IntPtr.Zero))
                                {
                                    if (!Native.PostMessage(lastParent, (uint)Native.WM_KEYUP, key, IntPtr.Zero))
                                        MessageBox.Show("Code " + Marshal.GetLastWin32Error(), "Error PostMessage failed");
                                }
                                else
                                    MessageBox.Show("Code " + Marshal.GetLastWin32Error(), "Error PostMessage failed");
                            }
                        }
                    }
                    while (gsHwndNotFound);
                }
            }
            else
                return false;
            return gsHwndNotFound;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            // keep as member variable so it doesnt get gc'ed
            enumWindowsProc = new Native.EnumWindowsProc(EnumWindows);
            enumChildWindowsProc = new Native.EnumWindowsProc(EnumChildWindows);
            // find the gs window
            gsHwndNotFound = true;
            Native.EnumWindows(enumWindowsProc, IntPtr.Zero);
        }
    }
}
